package com.example.service.impl;

import com.example.mapper.WorkExceptionRemovalMapp;
import com.example.pojo.exceptionDataPojo;
import com.example.pojo.problemDataPojo;
import com.example.service.WorkExceptionRemovalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * ClassName:WorkExceptionRemovalImpl
 * Description:
 *
 * @Author 赵永峰
 * @Create 2025/3/3 18:08
 * @Version 1.0
 */
@Service
@Slf4j
public class WorkExceptionRemovalImpl implements WorkExceptionRemovalService {
    //电脑端插入一行报警\异常解除的信息
    @Autowired
    private WorkExceptionRemovalMapp workExceptionRemovalMapp;
    public List<exceptionDataPojo> excInsertOne(exceptionDataPojo exceptionDataPojo){
        //保存数据
        exceptionDataPojo.setEstablishTime(LocalDateTime.now());
        exceptionDataPojo.setUpdateTime(LocalDateTime.now());
        workExceptionRemovalMapp.excInsertOne(exceptionDataPojo);
        //把所有的数据查出来，再反馈给前端
        List<exceptionDataPojo> exceptionDataPojos= workExceptionRemovalMapp.inquiryAll();
        return exceptionDataPojos;
    }
    //查询所有的报警、异常数据

    public List<exceptionDataPojo> inquiryAll(){
        List<exceptionDataPojo> exceptionDataPojos=workExceptionRemovalMapp.inquiryAll();
        return exceptionDataPojos;
    }
    //向问题数据库插入一条数据
    public void problemInserOne(problemDataPojo problemDataPojo){
        //时间赋值
        problemDataPojo.setUpdateTime(LocalDateTime.now());
        problemDataPojo.setEstablishTime(LocalDateTime.now());
        workExceptionRemovalMapp.problemInserOne(problemDataPojo);
    }
    //数据赋值，向后端筛选是“机”的所有问题，需要的是一个根据一个字段进行筛选
    public List<problemDataPojo> inquiryProblemOneFiled(Map<String, Object> map){
        String firstFiledName=(String) map.get("firstFiledName");
        String firstFiledVal=(String) map.get("firstFiledVal");
        String secondFiledName=(String) map.get("secondFiledName");
        String secondFiledVal=(String) map.get("secondFiledVal");
            List<problemDataPojo> pojos=workExceptionRemovalMapp.inquiryProblemOneFiled(firstFiledName,firstFiledVal);
            return pojos;
    }
    //辅助函数：对报警/异常问题库的一个行的某一个字段进行修改,修改的值是数字
    public void editOneFiledExceptionDataInt(Map<String, Object> map){
        Integer id=(Integer) map.get("id");
        String firstFiledName=(String) map.get("firstFiledName");
        int firstFiledVal=(int) map.get("firstFiledVal");
        workExceptionRemovalMapp.editOneFiledExceptionDataInt(id,firstFiledName,firstFiledVal);
    }
    //辅助函数：对问题库，修改一个字段，是字符串
    public void editOneFiledStr(Map<String, Object> map){
        System.out.println(map);
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        Integer id=(Integer) map.get("id");
        System.out.println(id);
        System.out.println(filedName);
        System.out.println(filedVal);
        workExceptionRemovalMapp.editOneFiledStr(filedName,filedVal,id);
    }
    //辅助函数：对问题库，修改一个字段，是数字
    public void editOneFiledInt(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        int filedVal=(int) map.get("filedVal");
        Integer id=(Integer) map.get("id");
        workExceptionRemovalMapp.editOneFiledInt(filedName,filedVal,id);
    }
    //根据ID，修改一行报警数据,所有的字段都修改
    public void excEditOne(exceptionDataPojo pojo){
        pojo.setUpdateTime(LocalDateTime.now());
        workExceptionRemovalMapp.excEditOne(pojo);
    }
    //辅助函数：对报警/异常信息表中，对一个日期类型的数据，筛选≥某一个值，和＜某一个值，
    public List<exceptionDataPojo> queryExceptionOneFiledScope(Map<String, Object> map){
        String filedName=(String) map.get("filedName");

        // 获取字符串日期值并解析为 LocalDateTime
        String minValStr = (String) map.get("minVal");
        String maxValStr = (String) map.get("maxVal");
        System.out.println();
        // 解析日期字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime minVal = LocalDateTime.parse(minValStr, formatter);
        LocalDateTime maxVal = LocalDateTime.parse(maxValStr, formatter);
        List<exceptionDataPojo> pojos=workExceptionRemovalMapp.queryExceptionOneFiledScope(filedName,minVal,maxVal);
        return pojos;
    }
}
